//
//  ContactView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/29.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class ContactView: UIView {

    fileprivate let cellId = "contactCell"
    fileprivate let dataArray = [wechatNumber,enterpriseQQ,sinaWeibo,officialWebsite]
    fileprivate let images = [#imageLiteral(resourceName: "weixin_logo"),#imageLiteral(resourceName: "qq_logo"),#imageLiteral(resourceName: "sina_logo"),#imageLiteral(resourceName: "ic_launcher")]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.mineColor()
        
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        self.addSubview(headView)
        
        let serviceImage = UIImageView()
        serviceImage.image = #imageLiteral(resourceName: "custom_service")
        headView.addSubview(serviceImage)
        
        let hotlineLabel = UILabel()
        hotlineLabel.text = serviceHotline
        hotlineLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        headView.addSubview(hotlineLabel)
        
        let timeLabel = UILabel()
        timeLabel.text = serviceTime
        timeLabel.textColor = UIColor.gray
        timeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        headView.addSubview(timeLabel)
        
        let phoneLabel = UILabel()
        phoneLabel.text = servicePhone
        phoneLabel.textColor = UIColor.phoneColor()
        phoneLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        headView.addSubview(phoneLabel)
        
        let dialButton = UIButton()
        dialButton.backgroundColor = UIColor.phoneColor()
        dialButton.setTitle(dial, for: .normal)
        dialButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        dialButton.layer.cornerRadius = 5
        dialButton.layer.masksToBounds = true
        dialButton.addTarget(self, action: #selector(dialServiceHotline), for: .touchUpInside)
        headView.addSubview(dialButton)
        
        let tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.bounces = false
        tableView.tableFooterView = UIView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        headView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.height.equalTo(RATIO_H(maxNum: 120))
        }
        
        serviceImage.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        hotlineLabel.snp.makeConstraints { (make) in
            make.left.equalTo(serviceImage.snp.right).offset(RATIO(maxNum: 10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(hotlineLabel)
            make.top.equalTo(hotlineLabel.snp.bottom)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(hotlineLabel)
        }
        
        phoneLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        dialButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalTo(phoneLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    //MARK: 拨打客服电话
    @objc fileprivate func dialServiceHotline() {
        
        UIApplication.shared.openURL(NSURL(string: "telprompt://4000079631")! as URL)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

extension ContactView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = dataArray[indexPath.row]
        cell.imageView?.image = images[indexPath.row]
        cell.accessoryType = .disclosureIndicator
        cell.selectionStyle = .none
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
